/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.ufc.quixada.adrs.grafico;

import br.ufc.quixada.adrs.model.RelatorioQualitativoResult;
import br.ufc.quixada.adrs.util.Msg;
import java.awt.BasicStroke;
import java.awt.Color;
import java.text.DecimalFormat;
import java.util.List;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.AxisLocation;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.labels.ItemLabelAnchor;
import org.jfree.chart.labels.ItemLabelPosition;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.labels.StandardCategoryToolTipGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.ui.TextAnchor;

/**
 * A simple demonstration application showing how to create a horizontal bar
 * chart.
 */
public class ADRSBarChart {

    private String title;
    private String labelXAxis;
    private String labelYAxis;
    private String graphicType;

    public ADRSBarChart(String _title, String _labelXAxis, String _labelYAxiss, String _graphicType) {

        this.title = _title;
        this.labelXAxis = _labelXAxis;
        this.labelYAxis = _labelYAxiss;
        this.graphicType = _graphicType;
    }

    /**
     * Creates a sample dataset.
     *
     * @return A sample dataset.
     */
    public JFreeChart createChart(CategoryDataset dataset) {
        // create the chart...
        JFreeChart chart = ChartFactory.createBarChart3D(
                title, // chart title
                labelXAxis, // domain axis label
                labelYAxis, // range axis label
                dataset, // data
                PlotOrientation.VERTICAL, // orientation
                true, // include legend
                true,
                false);
//        chart.addSubtitle(new TextTitle(
//                "Fonte: Sistema ADRS",
//                new Font("Dialog", Font.ITALIC, 10)));
        CategoryPlot plot = (CategoryPlot) chart.getPlot();
        plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_LEFT);
        BarRenderer renderer = (BarRenderer) plot.getRenderer();

        renderer.setItemLabelAnchorOffset(6.0);
        renderer.setBaseItemLabelsVisible(true);
        renderer.setBaseItemLabelGenerator(
                new StandardCategoryItemLabelGenerator());
        renderer.setBaseToolTipGenerator(new StandardCategoryToolTipGenerator(
                "{0}, {1}) = {4} per 100,000", new DecimalFormat("0")));
//       Por default, quando os numeros não podem ser exibidos dentro da barra eles são ocultados.
//       Para exibir, esses numeros devemos criar uma posição que os eleva ao topo. em seguida usamos//        o metodo setPositiveItemLabelPositionFallback(position) para elevar o label para o topo.
        ItemLabelPosition position = new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_CENTER);
        renderer.setPositiveItemLabelPositionFallback(position);

        renderer.setMinimumBarLength(0.10);
        renderer.setMaximumBarWidth(0.10);
        renderer.setDrawBarOutline(true);
        renderer.setBaseOutlinePaint(Color.white);
        renderer.setBaseOutlineStroke(new BasicStroke(0.5f));
        CategoryAxis categoryAxis = plot.getDomainAxis();
        //categoryAxis.setItemMargin(0.20);
        categoryAxis.setUpperMargin(0.02);
        categoryAxis.setLowerMargin(0.02);
//        //categoryAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_90);
        categoryAxis.setMaximumCategoryLabelLines(100);
        NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
        rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
//        rangeAxis.setUpperMargin(0.10);
//        renderer.setMaximumBarWidth(0.2);
        renderer.setItemMargin(0.05);

        if (graphicType.equals(Msg.GRAPHIC_QT_MOVIMENTACOES) || graphicType.equals(Msg.GRAPHIC_QT_MOVIMENTACOES_GERAL)) {
            List list = dataset.getRowKeys();
            int serieIndex = 0;
            for (int i = 0; i < list.size(); i++) {
                String serie = (String) list.get(i);
                if (serie.equalsIgnoreCase("Comprados")) {
                    serieIndex = dataset.getRowIndex(serie);
                    renderer.setSeriesPaint(serieIndex, Color.BLUE);
                } else if (serie.equalsIgnoreCase("Mortos")) {
                    serieIndex = dataset.getRowIndex(serie);
                    renderer.setSeriesPaint(serieIndex, Color.BLACK);
                    renderer.setSeriesItemLabelPaint(serieIndex, Color.WHITE);
                } else if (serie.equalsIgnoreCase("Nascidos")) {
                    serieIndex = dataset.getRowIndex(serie);
                    renderer.setSeriesPaint(serieIndex, Color.WHITE);
                } else if (serie.equalsIgnoreCase("Roubados")) {
                    serieIndex = dataset.getRowIndex(serie);
                    renderer.setSeriesPaint(serieIndex, Color.RED);
                } else if (serie.equalsIgnoreCase("Vendidos")) {
                    serieIndex = dataset.getRowIndex(serie);
                    renderer.setSeriesPaint(serieIndex, Color.GREEN);
                }
            }
        } else if (graphicType.equals(Msg.GRAPHIC_QS_DOENCAS_GERAL)) {
            renderer.setSeriesPaint(0, Color.BLUE);
            chart.getLegend().setVisible(false);
        } else if (graphicType.equals(Msg.GRAPHIC_QT_DESCARTES_GERAL)) {
            List seriesName = dataset.getRowKeys();
            for (int i = 0; i < seriesName.size(); i++) {
                String serieName = (String) seriesName.get(i);
                if (serieName.equalsIgnoreCase("Descarte de Fêmeas")) {
                    renderer.setSeriesPaint(i, Color.green);
                } else if (serieName.equalsIgnoreCase("Descarte de Machos")) {
                    renderer.setSeriesPaint(i, Color.blue);
                }
            }
        } else if (graphicType.equals(Msg.GRAPHIC_QL_TIPO_RESPOSTA) || graphicType.equals(Msg.GRAPHIC_QL_AVALIACAO)) {
            List seriesName = dataset.getRowKeys();
            for (int i = 0; i < seriesName.size(); i++) {
                String serie = (String) seriesName.get(i);
                if (serie.equalsIgnoreCase(RelatorioQualitativoResult.RESPOSTA_SIM)) {
                    renderer.setSeriesPaint(i, Color.BLUE);
                } else if (serie.equalsIgnoreCase(RelatorioQualitativoResult.RESPOSTA_SIM_ADEQUADAMENTE)) {
                    renderer.setSeriesPaint(i, Color.GREEN);
                } else if (serie.equalsIgnoreCase(RelatorioQualitativoResult.RESPOSTA_NAO)) {
                    renderer.setSeriesPaint(i, Color.RED);
                } else if (serie.equalsIgnoreCase(RelatorioQualitativoResult.RESPOSTA_NAO_RESPONDIDO)) {
                    renderer.setSeriesPaint(i, Color.GRAY);
                }
            }
        }
//        renderer.setMaximumBarWidth(0.10);
        renderer.setBaseSeriesVisible(true);


        return chart;

    }
}
